package club.beimeng.eduservice.controller;


import club.beimeng.commonutils.R;
import club.beimeng.eduservice.entity.CoursePublish.CoursePublishVo;
import club.beimeng.eduservice.entity.EduCourse;
import club.beimeng.eduservice.entity.vo.CourseInfoQuery;
import club.beimeng.eduservice.entity.vo.CourseQuery;
import club.beimeng.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-25
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin /*跨域问题*/
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoQuery infoQuery){
        String id = courseService.saveCourseInfo(infoQuery);
        return R.ok().data("courseId",id);
    }

    // 根据课程id查询课程基本信息
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoQuery answer = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo",answer);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoQuery courseInfo){
        courseService.updateCourseInfo(courseInfo);
        return R.ok();
    }

    // 根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId){
        CoursePublishVo coursePublishVo = courseService.getPublishCourseInfo(courseId);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //根据id进行发布
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        boolean b = courseService.updateById(eduCourse);
        return b ? R.ok() : R.error();
    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("{page}/{limit}")
    public R pageQuery(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
                       @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
                       @ApiParam(name = "courseQuery", value = "查询对象", required = false) CourseQuery courseQuery){
        Page<EduCourse> pageParam = new Page<>(page, limit);
        courseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  R.ok().data("total", total).data("rows", records);
    }

    //根据id删除课程
    @ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id", value = "课程ID", required = true)
        @PathVariable String id){
        boolean result = courseService.removeCourseById(id);
        if(result){
            return R.ok();
        }else{
            return R.error().message("删除失败");
        }
    }
}

